<template>
    <van-tabs v-model="tabs.index" color="#00CC00" class="order-list-items fix-order-listing">
        <van-tab title="待上交">
            <van-list v-model="handin.loading" :finished="handin.finished" finished-text="没有更多了" @load="onLoad(handin.textType)">
                <normal-order-hanin-list v-for="(item, index) in handin.dataset" :key="index"
                    @onSelectOperate="eventOnItemSelectOperate"
                    :ibase="item.base" :dataset="item.items" :index="index" :allSelect="handin.allSelectState" class="el-item"
                />
            </van-list>
            <div class="page-bottom-bar">
                <div class="flex-item check-all-select">
                    <van-checkbox v-model="handin.allSelectState">全选</van-checkbox>
                </div>
                <div class="flex-item tip-message">预估报价：<span>￥</span><span>{{ handin.orderPrice }}</span></div>
                <div class="flex-item">
                    <span @click="eventClickSubmitHandin" class="bar-button bar-button-green">去上交</span>
                </div>
            </div>
        </van-tab>
        <van-tab title="待确认">
            <van-list v-model="confirm.loading" :finished="confirm.finished" finished-text="没有更多了" @load="onLoad(confirm.textType)">
                <normal-order-list v-for="(item, index) in confirm.dataset" :key="index"
                    @onCancelOrder="eventOnCancelOrderItem"
                    :ibase="item.base" :dataset="item.items" :textType="confirm.textType" :index="index" class="el-item"
                />
            </van-list>
        </van-tab>
        <van-tab title="已上交">
            <van-list v-model="complete.loading" :finished="complete.finished" finished-text="没有更多了" @load="onLoad(complete.textType)">
                <normal-order-list v-for="(item, index) in complete.dataset" :key="index"
                    :ibase="item.base" :dataset="item.items" :textType="complete.textType" class="el-item"
                />
            </van-list>
        </van-tab>
        <van-tab title="已取消">
            <van-list v-model="cancel.loading" :finished="cancel.finished" finished-text="没有更多了" @load="onLoad(cancel.textType)">
                <normal-order-list v-for="(item, index) in cancel.dataset" :key="index"
                    :ibase="item.base" :dataset="item.items" :textType="cancel.textType" class="el-item"
                />
            </van-list>
        </van-tab>
    </van-tabs>
</template>

<script>
    import {
        Tab, Tabs, List,
        Checkbox
    } from 'vant';

    import NormalOrderList from '../../components/currency/order-item/normal-list-salesman.vue';
    import NormalOrderHaninList from '../../components/currency/order-item/normal-list-salesman-hanin.vue';

    export default {
        name: 'SalesmanOrderListing',
        components:{
            [Tab.name]: Tab, [Tabs.name]: Tabs, [List.name]: List,
            [Checkbox.name]: Checkbox,
            NormalOrderList, NormalOrderHaninList
        },
        data() {
            return {
                tabs: {
                    index: 0
                },
                handin: {
                    allSelectState: false, selectItems: [], orderPrice: 0,
                    page: 0, list_rows: 10,
                    loading: false, finished: false,
                    textType: 'handin',
                    dataset: []
                },
                confirm: {
                    page: 0, list_rows: 10,
                    loading: false, finished: false,
                    textType: 'wait-confirm',
                    dataset: []
                },
                complete: {
                    page: 0, list_rows: 10,
                    loading: false, finished: false,
                    textType: 'complete',
                    dataset: []
                },
                cancel: {
                    page: 0, list_rows: 10,
                    loading: false, finished: false,
                    textType: 'cancel',
                    dataset: []
                }
            }
        },
        mounted() {
            if(this.$route.query.index) this.tabs.index = this.$route.query.index;
        },
        methods: {
            getOrderList(items) {
                var that = this, status = '';

                switch (items.textType) {
                    case 'handin':
                        status = 5;
                        break;
                    case 'confirm':
                    case 'wait-confirm':
                        status = 2;
                        break;
                    case 'complete':
                        status = 5;
                        break;
                    case 'cancel':
                        status = 6;
                        break;
                }
                var url = '/api/home/order/getlist',
                    params = {
                        page: items.page,
                        list_rows: items.list_rows,
                        status: status,
                        // shop_id: '',
                        token: that.$store.state.options.user.token
                    };
                if(items.textType == 'handin') params.is_shangjiao = 0;

                var detail = [];
                this.$util.postRequestInterface(url, params, (res) => {
                    items.page = items.page + 1;
                    if (res.data == '') {
                        items.loading = false;
                        items.finished = true;
                    } else {
                        for (let i of res.data.data) {
                            for (let x of i.detail) {
                                detail.push({
                                    title: x.garbagename,
                                    // number: x.weighting_method == 1 ? x.weighting_num : '',
                                    // weight: x.weighting_method == 0 ? x.weighting_num : '',
                                    number: x.weighting_num,
                                    unit: x.danweiming,
                                    time: that.$moment(i.create_time * 1000).format('YYYY-MM-DD'),
                                });
                            }
                            items.dataset.push({
                                base: {
                                    id: i.id,
                                    number: i.ordernumber,
                                    amount: i.price
                                },
                                items: detail
                            });
                            detail = [];
                        }
                        // // 加载状态结束
                        items.loading = false;
                        // // 数据全部加载完成
                        if (items.dataset.length >= res.data.count) {
                            items.finished = true;
                        }
                    }
                });
            },
            onLoad(textType) {
                switch (textType) {
                    case 'handin':
                        this.getOrderList(this.handin);
                        break;
                    case 'confirm':
                    case 'wait-confirm':
                        this.getOrderList(this.confirm);
                        break;
                    case 'complete':
                        this.getOrderList(this.complete);
                        break;
                    case 'cancel':
                        this.getOrderList(this.cancel);
                        break;
                }
            },
            eventClickSubmitHandin() {
                this.$router.push({
                    name: 'SalesmanHandInOrderConfirm',
                    params: {
                        items: this.handin.selectItems
                    }
                });
            },
            eventOnItemSelectOperate(params) {
                if(params.select) {
                    this.handin.selectItems.push(this.handin.dataset[params.index]);
                } else {
                    for(let i in this.handin.selectItems) {
                        if(this.handin.selectItems[i].base.number == this.handin.dataset[params.index].base.number) {
                            this.handin.selectItems.splice(i, 1);
                            break;
                        }
                    }
                }

                var amount = 0;
                for(let x of this.handin.selectItems) {
                    amount += parseFloat(x.base.amount);
                }
                this.handin.orderPrice = amount.toFixed(2);
            },
            eventOnCancelOrderItem(index) {
                this.confirm.dataset.splice(index, 1);
            }
        }
    }
</script>

<style lang='scss' scoped>
    @import "../../libs/styles/bottom-fixed-bar.scss";

    .order-list-items {

        .el-item {
            padding: 0.5rem 1rem 0rem;
        }
    }

    .tip-message {
        text-align: right !important;
        justify-content: flex-end !important;
        padding-right: 3.2rem;

        span {
            color: #FF0000;
        }

        span:first-child {
            font-size: 1.3rem;
        }

        span:last-child {
            font-size: 1.9rem;
        }
    }
</style>
